home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13530 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to detect if a number has fractions
  5. Date: 8 Apr 96 15:00:11 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.828975611@hubcap>
  8. References: <4kb1p8$gph@gate.compart.fi>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10. X-Newsreader: NN version 6.5.0 #1
  11.  
  12. joonas.kervinen@pcb.compart.fi (joonas kervinen) writes:
  13.  
  14. >Here's a small detail (not quite Win spesific, sorry!) that's bugging
  15.  
  16. Of course, in comp.lang.c, we prefer non-Win-specific questions.
  17.  
  18. >me: How do I detect in Borland C (BCC4.5 or 5.0) whether a double
  19. >value has fractions. And I mean quickly, not using time consuming
  20. >floating point functions.
  21.  
  22. In ANSI/ISO C, you can call the modf() function, which will work
  23. reliably for any double value.  If you don't like that, you can simply
  24. compare the value cast to an integer type with the original value,
  25. but then you need to worry about whether the cast value fits in 
  26. the integer type:
  27.  
  28.     /* This works only if LONG_MIN <= x && x <= LONG_MAX */
  29.  
  30.     double x;
  31.  
  32.     if ( (long) x == x )
  33.       puts("x is integral");
  34.     else
  35.       puts("x is not integral");
  36.  
  37. It's not necessarily clear which will be faster on a particular
  38. system--changes in representation can be as slow as floating-point
  39. manipulations.  
  40.  
  41. I don't know if BCC offers any other specialized facilities for
  42. accomplishing this task, but I doubt it.  Besides, why use
  43. non-portable methods to accomplish tasks that can be accomplished
  44. portably (unless the performance penalty is huge)?
  45.  
  46. >Please answer via e-mail.
  47.  
  48. If you can't be bothered to scan for responses to your own message, you
  49. deserve not to see them.  If you think the request will generate more
  50. traffic than it's worth, at least offer to summarize.
  51. -- 
  52.         Matthew Saltzman
  53.         Clemson University Math Sciences
  54.         mjs@clemson.edu
  55.